package com.mail.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mail.dubbo.mapper.PmsProductAttributeCategoryMapper;
import com.mail.dubbo.mapper.PmsProductAttributeMapper;
import com.mail.dubbo.mapper.PmsProductCategoryAttributeRelationMapper;
import com.mail.model.db.PmsProductAttribute;
import com.mail.model.db.PmsProductAttributeCategory;
import com.mail.model.db.PmsProductCategoryAttributeRelation;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@DubboService
public class PmsProductAttributeApiImpl implements PmsProductAttributeApi {

    @Autowired
    private PmsProductAttributeMapper productAttributeMapper;
    @Autowired
    private PmsProductAttributeCategoryMapper productAttributeCategoryMapper;
    @Autowired
    private PmsProductCategoryAttributeRelationMapper productCategoryAttributeRelationMapper;

    // 根据商品分类的id获取商品属性及属性分类
    @Override
    public List<Map<String, Integer>> queryProductAttributeAndCategory(Integer productCategoryId) {
        List<Map<String, Integer>> mapList = new ArrayList<>();
        LambdaQueryWrapper<PmsProductCategoryAttributeRelation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PmsProductCategoryAttributeRelation::getProductCategoryId, productCategoryId);
        List<PmsProductCategoryAttributeRelation> pmsProductCategoryAttributeRelations = productCategoryAttributeRelationMapper.selectList(wrapper);
        for (PmsProductCategoryAttributeRelation pmsProductCategoryAttributeRelation : pmsProductCategoryAttributeRelations) {
            Map<String, Integer> map = new HashMap<>();
            LambdaQueryWrapper<PmsProductAttribute> queryWrapper = new LambdaQueryWrapper<>();
            Long productAttributeId = pmsProductCategoryAttributeRelation.getProductAttributeId();
            queryWrapper.eq(PmsProductAttribute::getId, productAttributeId);
            PmsProductAttribute productAttribute = productAttributeMapper.selectOne(queryWrapper);
            Long productAttributeCategoryId = productAttribute.getProductAttributeCategoryId();
            map.put("attributeId", Integer.valueOf(productAttributeId.toString()));
            map.put("attributeCategoryId", Integer.valueOf(productAttributeCategoryId.toString()));
            mapList.add(map);
        }
        return mapList;
    }

    // 添加商品属性信息
    @Override
    public void createProductAttribute(PmsProductAttribute productAttribute) {
        productAttributeMapper.insert(productAttribute);
    }

    // 批量删除商品属性
    @Override
    public void deleteAttributeByIds(List<Long> deleteIds) {
        // 删除属性表中相应的数据
        LambdaQueryWrapper<PmsProductAttribute> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(PmsProductAttribute::getId, deleteIds);
        productAttributeMapper.delete(wrapper);
        // 删除属性分类联系表
        LambdaQueryWrapper<PmsProductCategoryAttributeRelation> relationWrapper = new LambdaQueryWrapper<>();
        relationWrapper.in(PmsProductCategoryAttributeRelation::getProductAttributeId, deleteIds);
        productCategoryAttributeRelationMapper.delete(relationWrapper);
    }

    // 根据分类查询属性列表或参数列表
    @Override
    public Page<PmsProductAttribute> queryAttributeList(Integer cid, Integer type, Integer pageNum, Integer pageSize) {
        Page<PmsProductAttribute> pageInfo = new Page<>(pageNum, pageSize);
//        LambdaQueryWrapper<PmsProductAttributeCategory> categoryWrapper = new LambdaQueryWrapper<>();
//        categoryWrapper.eq(PmsProductAttributeCategory::getId, Long.valueOf(cid));
//        PmsProductAttributeCategory productAttributeCategory = productAttributeCategoryMapper.selectOne(categoryWrapper);
        LambdaQueryWrapper<PmsProductAttribute> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PmsProductAttribute::getProductAttributeCategoryId,Long.valueOf(cid));
        wrapper.eq(PmsProductAttribute::getType,type);
        Page<PmsProductAttribute> page = productAttributeMapper.selectPage(pageInfo, wrapper);
        return page;

    }

    // 修改商品属性信息
    @Override
    public void updateProductAttribute(Long id, PmsProductAttribute productAttributeParam) {
        LambdaQueryWrapper<PmsProductAttribute> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PmsProductAttribute::getId, id);
        productAttributeMapper.update(productAttributeParam, wrapper);
    }

    // 查询单个商品属性
    @Override
    public PmsProductAttribute queryProductAttributeById(Long id) {
        LambdaQueryWrapper<PmsProductAttribute> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PmsProductAttribute::getId, id);
        PmsProductAttribute productAttribute = productAttributeMapper.selectOne(wrapper);
        return productAttribute;
    }

}
